<article class="module width_full">
    <header><h3 class="tabs_involved">DANH MỤC THỂ LOẠI SÁCH</h3>
        <ul class="tabs">
            <li><a href="#tab1">Tất cả</a></li>
        </ul>
    </header>
    <div class="tab_container"  style="overflow-y:scroll; height:200px;width: 100%;">
        <div id="tab1" class="tab_content">
            <table class="table demo default  footable tablesorter table-hover" cellspacing="0" > 
                <thead> 
                    <tr> 
                        <th data-type="numeric" data-sort-initial="true" >STT</th> 
                        <th  >Mã loại</th> 
                        <th >Tên loại </th> 
                        <th data-sort-ignore="true">Hành động</th> 
                    </tr> 
                </thead> 
                <tbody id="category"> 
                    <?php
                    $i = 0;

                    foreach ($theloais as $value) {

                        $i+=1;
                        echo "<tr id='row" . $value->MATL . "'>";
                        echo '<td>' . $i . '</td>';
                        echo '<td class="td_matl">' . $value->MATL . '</td>';
                        echo '<td class="td_tentl">' . $value->TENTL . '</td>';
                        echo "<td><a class='edit-category' href='" . $value->MATL . "'><input type='image' src='" . base_url() . "public/admin/images/icn_edit.png' title='Chỉnh sửa thể loại sách '></a>
					<a class='delete-cate' href='" . $value->MATL . "'><input type='image' src='" . base_url() . "public/admin/images/icn_trash.png'title='thể loại sách'></a>
					<a class='add-cate'><input type='image' src='" . base_url() . "public/admin/images/icn_add_user.png' title='Thêm loại sách mới'></a></td>";
                        echo '</tr>';
                    }
                    ?>
                </tbody> 
            </table>
        </div><!-- end of #tab1 -->






    </div><!-- end of .tab_container -->

</article><!-- end of content manager article -->	

<!--Begin add new--> 
<div class="modal fade" id="myModal-add" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel"> Thêm thể loại </h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form">                  
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">Tên thể loại</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control tentl"  id="tentl-add" name="tentl" placeholder="Tên thể loại" required="true">
                        </div>
                    </div>                                  
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" id="btn-add" class="btn btn-primary">Lưu</button>
            </div>
        </div>
    </div>
</div>
<!--End add new-->
<!--Begin Edit-->
<div class="modal fade" id="myModal-edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel"> Chỉnh sửa thể loại </h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form">
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">Mã thể loại</label>
                        <div class="col-sm-10">                            
                            <input type="text" class="form-control matl"  id="matl-edit" name="matl" readonly="true" >
                        </div>
                    </div>  
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">Tên thể loại</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control tennxb"  id="tentl-edit" name="tentl" placeholder="Tên nhà xuất bản">
                        </div>
                    </div>                                  
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" id="btn-save" class="btn btn-primary">Lưu</button>
            </div>
        </div>
    </div>
</div>
<!--End Edit-->
<script>
    $(document).ready(function() {
        $('.edit-category').click(function(e) {
            e.preventDefault();

            var matl = $(this).attr("href");
            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/GetTL',
                data: {
                    matl: matl
                },
                success: function(data) {
                    $("#myModal-edit").modal();
                    $("#myModal-edit #matl-edit").val(data.matl);
                    $("#myModal-edit #tentl-edit").val(data.tentl);

                }, error: function(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }, dataType: "json"
            });
        });
        $("#btn-add").click(function(e) {
            e.preventDefault();
            var tentl = $("#tentl-add").val();
            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/theloai_add',
                data: {
                    tentl: tentl
                },
                success: function(data) {
                    $("#myModal-add").modal('hide');
                }, error: function(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            });
        });
        $("#btn-save").click(function(e) {
            e.preventDefault();
            var matl = $('#matl-edit').val();
            var tentl = $('#tentl-edit').val();

            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/theloai_upd',
                data: {
                    matl: matl,
                    tentl: tentl
                },
                success: function(data) {
                   // alert($('#row' + matl).find(".td_matl").text());
                    $('#myModal-edit').modal('hide');
                    $('#row' + matl).find(".td_matl").text(data.matl);
                    $('#row' + matl).find(".td_tentl").text(data.tentl);


                }, error: function(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }, dataType: "json"
            });
        });
        $('.delete-cate').click(function(e) {
            e.preventDefault();
            var matl = $(this).attr("href");
            alert(matl);
            if (confirm('Bạn có muốn xóa thể loại sách ?')) {
                $.ajax({
                    type: "POST",
                    url: globalurl + 'index.php/admin/theloai_del',
                    data: {
                        matl: matl
                    },
                    success: function(data) {                     
                        $('#row' + matl).remove();
                    }, error: function(xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                    }
                });
            }
        });

        $('.add-cate').click(function(e) {
            e.preventDefault();
            $("#myModal-add").modal();
        });
        $('#add_form').submit(function(e) {
            e.preventDefault();
            alert('d');
            var tentl = $('#catename').val();
            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/addnewtheloai',
                data: {
                    tentl: tentl
                },
                success: function(data) {
                    // $('#category').append(data);
                    alert(data);
                    $('#add_form').toggle("slow");
                }, error: function(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            });
        });
    });
</script>
